package com.qf.java2102.qihaonan.hotel.service.impl;

import com.qf.java2102.qihaonan.hotel.constant.MessageConstant;
import com.qf.java2102.qihaonan.hotel.dao.IFoodDao;
import com.qf.java2102.qihaonan.hotel.dao.impl.FoodDaoImpl;
import com.qf.java2102.qihaonan.hotel.entity.CartItem;
import com.qf.java2102.qihaonan.hotel.entity.ResultVO;
import com.qf.java2102.qihaonan.hotel.pojo.Food;
import com.qf.java2102.qihaonan.hotel.pojo.User;
import com.qf.java2102.qihaonan.hotel.service.ICartService;
import com.qf.java2102.qihaonan.hotel.utils.JdbcUtils;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.*;

/**
 * @author qhn
 * @version 1.0
 */
public class CartServiceImpl implements ICartService {

    private IFoodDao foodDao = new FoodDaoImpl();

    /**
     * {
     *      success : true,
     *      message : "查看餐车成功"
     *      data : {
     *         "cartItems" : List<CartItem>
     *         "totalMoney" : 1253.20
     *      }
     * }
     *
     * @return
     */
    @Override
    public ResultVO<Map<String, Object>> getCart(Map<Long, CartItem> cart, User user) {

        Map<String, Object> cartMap = new HashMap<>();
        try {
            //总计
            BigDecimal totalMoney = new BigDecimal(0);
            Integer totalNum = 0;

            if(cart == null) {
                //throw new RuntimeException("餐车中没有数据");
                return ResultVO.error("餐车中没有数据");
            }

            for (Long foodId : cart.keySet()) {
                Food food = foodDao.findById(foodId);
                CartItem cartItem = cart.get(foodId);
                cartItem.setFoodName(food.getFoodName());
                BigDecimal totalPrice = new BigDecimal(1);
                if(user.getIsMember().intValue() == 1) {
                    //会员价
                    cartItem.setPrice(food.getFoodMprice());
                } else {
                    //非会员价
                    cartItem.setPrice(food.getFoodPrice());
                }
                //小计  : 单价 * 数量
                totalPrice = cartItem.getPrice().multiply(new BigDecimal(cartItem.getNum()));
                cartItem.setTotalPrice(totalPrice);

                //总计金额计算
                totalMoney = totalMoney.add(cartItem.getTotalPrice());

                //总数量
                totalNum += cartItem.getNum();
            }

            //排序
            List<CartItem> cartList = new ArrayList<>( cart.values());

            //按时间排序
            Collections.sort(cartList, new Comparator<CartItem>() {
                @Override
                public int compare(CartItem o1, CartItem o2) {
                    return (int) (o2.getUpdateTime().getTime() - o1.getUpdateTime().getTime());
                }
            });

            cartMap.put("cartItems", cartList);
            cartMap.put("totalMoney", totalMoney);
            cartMap.put("totalNum", totalNum);
            return ResultVO.ok("查看餐车成功", cartMap);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.release(JdbcUtils.getConnection());
        }

        return ResultVO.error(MessageConstant.COMMON_ERROR);
    }
}
